package com.ssh.web.caction;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.entity.Staff;
import com.ssh.model.Constant;
import com.ssh.ser.comonstaff.StaffServiceI;

public class StaffLoginAction extends ActionSupport implements SessionAware {

	/**
	 *
	 */

	private static final long serialVersionUID = 1L;
	private String password;
	// For SessionAware
	private Map<String, Object> session;
	private String staffId;
	private StaffServiceI staffService;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return login();
	}

	public String getPassword() {
		return password;
	}

	public String getStaffId() {
		return staffId;
	}

	public StaffServiceI getStaffService() {
		return staffService;
	}

	public String login() {
		/* STAFFID和PASSWORD为空则返回登录页面，否则验证登录 */
		try {
			if (null == staffId || null == password) {
				return "error";
			} else {

				System.out.println("staffId=" + staffId + " password=" + password
								+ " permission=" );
				Staff staff = staffService.login(staffId, password);
				if(staff == null) return "error";
				else if (staff.getPassword().equals(password)) {
					session.put("staffId", staffId);
					session.put("password", password);
					session.put("permission", staff.getStaffPermission());
					session.put("staff", staff);
					if(staff.getStaffPermission()==Constant.ADMIN)//管理员
						return "admin";
					if(staff.getStaffPermission()==Constant.COMMON_STAFF)//普通员工
						return "common";
					if(staff.getStaffPermission()==Constant.CONCIERGE)//门卫
						return "concierge";
					if(staff.getStaffPermission()==Constant.MANAGER)//manager
						return "manager";
					return "ok";
				} else {
					return "error";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}

	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public void setStaffService(StaffServiceI staffService) {
		this.staffService = staffService;
	}

}
